/*
 *领取优惠券列表
 */
require(['jquery','../modules/pageSize','../modules/popup', 'jqScroller'],function($,pageSetSize,popup){
	//设置html根目录字体大小
    setRootFontSize(pageSetSize);
    var curPagePos = 1;
	/*请求优惠券列表*/
	jqScroller.render = function(cpage){
        popup.loadPopup("正在加载中...");
        $.ajax({
            type: "GET",
            url: "/Home/NewUserCoupon/indexAPI"+getFixedExtraUrl(),
            data: { "page" : cpage},
            dataType: "json",
            success: function (rs) {
                popup.removeLoad();
				if(rs && rs.data != null)  {
                    renderCouponList(rs.data);
					curPagePos++;
                    jqScroller.curPage++;
                    jqScroller.unblock();
                }else{
					jqScroller.done();
                    if(curPagePos == 1){
                        $("#ajax-couponlist").html("<div class='no-data-txt'>没有数据！</div>");   
                    }else{                        
                        popup.alertPopup("没有更多数据！");
                    }                    
                }              
            },
            error: function () {
                popup.removeLoad();
                popup.alertPopup('服务器异常，请稍后重试！');
                jqScroller.unblock();
            }
        });
    };
    jqScroller.run();
    jqScroller.render(1);
	/*渲染优惠券列表*/
	function renderCouponList(list){
		var output = "";
		var c;
		for(var k in list){
			c = list[k];
			output += '<div class="oneitem" data-id="'+c["id"]+'">';
			output += '<div class="info">';
			output += '<img src="'+getDefaultImg(c["img"])+'" />';
			output += '<div class="dtbox">';
			output += '<span class="tit">'+c["name"]+'</span>';
			output += '<span class="desc">'+c["msg"]+'</span>';
			output += '</div>';
			output += '</div>';
			output += '<a href="javascript:;" class="view" onclick="showCouponDetail(this, event)">查看礼包&gt;</a>';

			//判断礼包是否开始或者结束
			if(c["dead"] == 1){
				output += '<a href="javascript:;" class="getBtn disable">已结束</a>';
			}else if(c["start"] == 1){
				output += '<a href="javascript:;" class="unstart">未开始</a>';
			}else {


				if(c["get"] == 1){
					output += '<a href="javascript:;" class="getBtn disable">已领</a>';
				}else{

					if(c["isNew"] == 0){
						output += '<a href="javascript:;" class="getBtn disable">限新人</a>';
					}else{
						if(c["get"] == 0){

							if(c["enough"] == 0){
								output += '<a href="javascript:;" class="getBtn disable">已抢光</a>';
							}else{
								output += '<a href="javascript:;" class="getBtn active" onclick="takeCoupon(this, event)">领取</a>';
							}

						}
					}


				}





			}




			output += '</div>';
		}
		if(curPagePos % 10 == 0){
			$("#ajax-couponlist").html(output);	
		}else{
			$("#ajax-couponlist").append(output);
		}
	}
});